package com.hua.springCloud.controller;


import com.hua.springCloud.entities.CommonResult;
import com.hua.springCloud.entities.Payment;
import com.hua.springCloud.service.PaymentService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@Slf4j
public class PaymentController {
    @Autowired
    private PaymentService paymentService;
    @Value("${server.port}")
    private  String port ;
    @Autowired
    private DiscoveryClient discoveryClient;
    @PostMapping("/payment/create")
    public CommonResult create(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("插入的结果为"+result);
            if(result>0){
              return new CommonResult(200,"插入成功,端口号为:"+port,result);
            }else {
                    return new CommonResult(444,"插入失败,端口号为:"+port,null);
            }
    }

    @GetMapping("/payment/get/{id}")
     public CommonResult<Payment>getPaymentById(@PathVariable("id")Long id){
        Payment paymentById = paymentService.getPaymentById(id);
        log.info("查询的结果为"+paymentById);
            if(paymentById!=null){
                    return new CommonResult(200,"查询成功,端口号为:"+port,paymentById);
            }{
                    return new CommonResult(444,"无该条记录,端口号为:"+port,null);
            }
    }
    @GetMapping("/info8001")
    public Object getInfoAboutThis(){
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("服务为:{}",service);
        }
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            log.info("服务名:{},端口号:{},ip地址:{},url地址:{}",
                    instance.getServiceId(),instance.getPort(),instance.getHost(),instance.getUri());
        }
        return discoveryClient;
    }
    @PostMapping("/info/forEntities")
    public CommonResult getInfo(@RequestBody Payment payment){
        int result = paymentService.create(payment);
        log.info("插入的结果为"+result);
        if(result>0){
            return new CommonResult(200,"插入成功,端口号为:"+port,result);
        }else {
            return new CommonResult(444,"插入失败,端口号为:"+port,null);
        }
    }
    @GetMapping("/getPaymentPort")
    public String getPort(){
        return port;
    }


    //测试fegin 超过1秒后，因为fegin默认等待1秒，超过1秒则报错
    @GetMapping("/getTiemOut")
    public String getTiemOut(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return port;
    }
    @GetMapping(value = "/payment/zipkin")
    public String paymentZipKin(){
        return "hi,I am payment_zipkin ";
    }
}
